<?php

namespace App\Http\Controllers\Admin;

use App\Models\Store;
use App\Models\Ticket;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class TicketController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function __construct()
    {
        view()->share([
            '_ts' => 'am-in',
            '_t' => 'am-active',
        ]);

    }

//门票管理首页
    public function index()
    {
//所有门票
        $tickets=Ticket::with('store')->paginate(10);

        return view('admin.ticket.index',compact('tickets'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    //新增页面
    public function create()
    {
        $cates=Store::all();
        return view('admin.ticket.create',compact('cates'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    //执行新增
    public function store(Request $request)
    {
        //字段较少，手动验证
        if(!$request->store_id or !$request->price){
            return back()->with('success','请填写完整');
        }
        //当前门票
        $ticket=Ticket::where('store_id',$request->store_id)->first();
        //一个门店只能有一种门票
        if($ticket){
            return back()->with('success','当前门店以有门票，如需修改请点击编辑');
        }
        Ticket::create($request->all());
        return redirect('admin/ticket')->with('success','新增门票成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    //编辑页面
    public function edit($id)
    {
        //所有门店
        $cates=Store::all();
        //当前门票
        $ticket=Ticket::find($id);
        return view('admin.ticket.edit',compact('ticket','cates'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    //执行编辑
    public function update(Request $request, $id)
    {
        //当前门票
        $ticket=Ticket::find($id);
        //字段较少，手动验证
        if(!$request->store_id or !$request->price){
            return back()->with('success','请填写完整');
        }else{
            $ticket->update($request->all());
            return redirect('admin/ticket')->with('success','修改成功');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {

    }
}
